<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +-----------------------------------------------------------  -----------+

require_once 'eleontev/HTML/BoxMsg.php';


// we combine all functions to get msg
// just for simplifing life in future
class AppMsg
{
	
	function getCommonMsgFile($file_name) {
		$file = array();
		$file['en']		= APP_MSG_DIR . 'en/' . $file_name;
		$file[APP_LANG]	= APP_MSG_DIR . APP_LANG . '/' . $file_name;
		
		foreach($file as $k => $v) {
			if(!file_exists($v)) { unset($file[$k]); }
		}
		
		return $file;
	}
	
	
	function getModuleMsgFile($module, $file_name) {
		return AppMsg::getCommonMsgFile($module . '/' . $file_name);
	}
	
	
	function parseMsgs($file, $section = false, $process_sections = 1, $parse_array = false) {
		$msg = array();
		$msg_array = array();
		
		if(is_array($file)) {
		
			foreach($file as $k => $v) {
				//$msg = array_merge($msg, GetMsgHelper::parseIni($v, $section, $process_sections));			
				
				if($parse_array) {
					$msg_array[$k] = GetMsgHelper::parseIni($v, $section, $process_sections);
				} else {
					$msg = GetMsgHelper::parseIni($v, $section, $process_sections) + $msg;
				}
			}
			
			// parse msg arrays
			if(isset($msg_array['en'])) {
				foreach($msg_array['en'] as $k => $v) {
					if(@!is_array($msg_array[APP_LANG][$k])) {
						$msg_array[APP_LANG][$k] = array();
					}
					$msg[$k] = $msg_array[APP_LANG][$k] + $msg_array['en'][$k];
				}			
			}
			
		} else {
			$msg = GetMsgHelper::parseIni($file, $section, $process_sections);
		}

		//echo "<pre>"; print_r($msg); echo "</pre>";
		return $msg;
	}
	
	
	function parseMsgsArray() {
		
	}
	
	
	function parseMsgsMultiIni($file, $section) {
		$msg = array();

		if(is_array($file)) {
			foreach($file as $k => $v) {
				if(file_exists($v)) {
					$msg = GetMsgHelper::parseMultiIni($v, $section);
				}
			}
		} elseif(file_exists($file)) {
			$msg = GetMsgHelper::parseMultiIni($file, $section);
		}
		
		return $msg;
	}
	
	
	// read common or module
	function getMsg($file_name, $module = false, $section = false, $process_sections = 1) {
		
		$file = ($module) ? AppMsg::getModuleMsgFile($module, $file_name)
		                  : AppMsg::getCommonMsgFile($file_name);
		
		return AppMsg::parseMsgs($file, $section, $process_sections);;
	}
	
	
	// read common + module if any
	function getMsgs($file_name, $module = false, $section = false, $process_sections = 1) {
	
		$file = AppMsg::getCommonMsgFile($file_name);		
		$msg = AppMsg::parseMsgs($file, $section, $process_sections);
		
		if($module) {
			$file = AppMsg::getModuleMsgFile($module, $file_name);
			$msg = array_merge($msg, AppMsg::parseMsgs($file, $section, $process_sections));
		}
		
		return $msg;
	}
	
	
	function getMenuMsgs($module = false, $section = false, $process_sections = 1) {
		return AppMsg::getMsg('menu_msg.ini', $module, $section, $process_sections);		
	}
	
	
	function getErrorMsgs($module = false, $section = false, $process_sections = 1) {
		return AppMsg::getMsgs('error_msg.ini', $module, $section, $process_sections);		
	}
	
	
	// this after wrong form submission
	function errorBox(&$errors, $module = false) {
		
		if(!$errors) { return; }
		
		/*
		if($key == 'raw') {
			list($file, $module, $section, $format) = explode(';', $errors[$key]);
			$msgs = AppMsg::getMsgs($file, $module, $section, 1);
			return BoxMsg::factory($format, $msgs);
		*/
		
		$key = key($errors);
		if($key === 'formatted') {
			return $errors[$key];
		
		} else {
		
			$msg = AppMsg::getErrorMsgs($module);
			foreach($errors as $k => $v) {
				$e[] = $msg[$v['msg']];
			}
			
			$msgs['title'] = $msg['error_title_msg'];
			$msgs['body'] = '<b>&raquo;</b> ' . implode('<br /><b>&raquo;</b> ', $e);
			return BoxMsg::factory('error', $msgs);
		}
	}	
	
	
	
	// mostly it will generate hint in admin area if specified
	function hintBox($keyword, $module = false) {
		
		$msg = array();
		$file = ($module) ? AppMsg::getModuleMsgFile($module, 'hint_msg.ini')
		                  : AppMsg::getCommonMsgFile('hint_msg.ini');

		$msg['body'] = AppMsg::parseMsgsMultiIni($file, $keyword);
		if(!empty($msg['body'])) {
			return BoxMsg::factory('hint', $msg);
		}
	}
	

	function serviceBox($keyword, $multi_ini = true) {
		
		$msg = array();
		$file = ($module) ? AppMsg::getModuleMsgFile($module, 'service_msg.ini')
		                  : AppMsg::getCommonMsgFile('service_msg.ini');

		$msg['body'] = AppMsg::parseMsgsMultiIni($file, $keyword);
		if(!empty($msg['body'])) {
			return BoxMsg::factory('hint', $msg);
		}
	}	
	
	
/*
	function wrongActionBox($keyword) {	
		
		$file = AppMsg::getCommonMsgFile('after_action_msg.ini');
		$msg =& GetMsgHelper::parseIni($file, $keyword, 1);
		
		if($msg) {
			return BoxMsg::factory('error', $msg);
		}
	}
*/
	
	
	function afterActionBox($keyword, $factory = 'error', $module = false) {
		
		$msg =& AppMsg::getMsgs('after_action_msg.ini', $module, $keyword, 1);
		if($msg) {
			return BoxMsg::factory($factory, $msg);
		}
	}
}
?>
